﻿ #define _CRT_SECURE_NO_WARNINGS 1
#include"Seqlist.h"

void Seqlistprint(SL* ps) {
	for (int i = 0; i <= ps->size; i++) {
		printf("%d ", ps->a[i]);
	}
};
void SeqlistInit(SL* ps) {
	ps->a = NULL;
	ps->size = ps->capacity = 0;
};
void SeqlistPushBack(SL* ps, SLDataType x) {
	SLcheckcapacity(ps);
	//realloc,malloc,calloc都是一种动态分配内存的函数，包含在头文件<stdlib.h>里
	//这个例子中相当于SLDataType类型的数组指针tmp获得了newcapacity乘以SLDataType类型的字节大小        

	ps->a[ps->size] = x;
	ps->size++;
};
void SeqlistDestory(SL* ps) {
	free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->size = 0;




}
void SLcheckcapacity(SL* ps) {
	if (ps->size == ps->capacity) {
		int newcapacity = 0 ? 4 : ps->capacity * 2;
		SLDataType* tmp = (SLDataType*)realloc(ps->a, newcapacity * sizeof(SLDataType));
		//realloc,malloc,calloc都是一种动态分配内存的函数，包含在头文件<stdlib.h>里
		//这个例子中相当于SLDataType类型的数组指针tmp获得了newcapacity乘以SLDataType类型的字节大小        
		if (tmp == NULL) {
			printf("分配内存失败");
			exit(-1);//退出的意思    


		}
		ps->a = tmp;
		ps->capacity = newcapacity;
	}
}
void SeqlistPopBack(SL* ps) {
	if (ps->size > 0) {
		ps->size--;

	}

	//arrest判断条件为真则继续运行，不然报错；要引入头文件<arrest.h>
};
void SeqlistPushFront(SL* ps, SLDataType x) {
	SLcheckcapacity(ps);
	int end = ps->size - 1;
	while (end >= 0) {
		ps->a[end + 1] = ps->a[end];
		ps->a[0] = x;
		ps->size++;

	}
};
void SeqlistPopFront(SL* ps) {
	int end = 1;
	if (ps->size >= 0) {

		ps->a[end - 1] = ps->a[end];
		end++;

	}
	ps->size--;
}
int SeqlistFind(SL* ps, SLDataType x) {
	int i = 0;
	while (i < ps->size) {
		if (x == ps->a[i]) {
			return 1;
		}
		if (i == ps->size) {
			return 0;
		}
	}




}
void SeqlistInsert(SL* ps, int pos, SLDataType x) {
	assert(ps->size > 0);
	int i = pos - 1;
	while (i > ps->size) {
		ps->a[pos] = ps->a[pos - 1];

		i++;
	}

	ps->a[pos - 1] = x;
	ps->size++;

}
void SeqlistErase(SL* ps,int pos) {
	assert(ps->size > 0);
	int i = pos - 1;
	while (i < ps->size) {
		ps->a[pos - 1] = ps->a[pos];

		i++;
	}
	ps->size--;
}

int main(void) {

	SL sl;
	SeqlistInit(&sl);
	SeqlistPushBack(&sl, 1);
	SeqlistPushBack(&sl, 2);
	SeqlistPushBack(&sl, 3);
	SeqlistPushBack(&sl, 4);
	SeqlistPushBack(&sl, 5);
	Seqlistprint(&sl);

	return 0;
}
